package com.czy.owner.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CaseShowModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/14.
 */
public class CaseShowAdapter extends RecyclerArrayAdapter<CaseShowModel.MyList> {
    private Context ctx;

    public CaseShowAdapter(Context context) {
        super(context);
        this.ctx = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new CaseShowViewHolder(parent, ctx);
    }

    class CaseShowViewHolder extends BaseViewHolder<CaseShowModel.MyList> {
        private TextView tv_case_show_title;
        private TextView tv_time;
        private ImageView img_case_show;
        private ImageView img_likes;
        private LinearLayout line_like;
        private TextView tv_like;
        private Context ctx;


        public CaseShowViewHolder(ViewGroup parent, Context ctx) {
            super(parent, R.layout.item_case_show);
            tv_case_show_title = $(R.id.tv_case_show_title);
            tv_time = $(R.id.tv_time);
            img_case_show = $(R.id.img_case_show);
            img_likes = $(R.id.img_likes);
            line_like = $(R.id.line_like);
            tv_like = $(R.id.tv_like);
            this.ctx = ctx;
        }

        @Override
        public void setData(final CaseShowModel.MyList model) {
            tv_case_show_title.setText(model.getTitle());
            String time = TimeUtils.formatTimeDifference2(Long.parseLong(model.getCreateTime()));
            tv_time.setText(time);

            try {
                getPicUrl(model);
                if (model.getResList().size() >= 1) {
                    GlideUtils.loadImage(ctx, listimg.get(0), img_case_show, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                        @Override
                        public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                        }

                        @Override
                        public void onLoadingError(String source, Exception e) {

                        }
                    },R.mipmap.default_icon,R.mipmap.default_icon);

                }
            } catch (Exception e) {

            }


            if (model.isPraise()) {
                img_likes.setImageResource(R.mipmap.icon_like_high);
                tv_like.setText("已赞");
                tv_like.setTextColor(ctx.getResources().getColor(R.color.appThemeColor));
            } else {
                img_likes.setImageResource(R.mipmap.icon_like_normal);
                tv_like.setText("点赞");
                tv_like.setTextColor(ctx.getResources().getColor(R.color.appThemeFontGray));
            }
            line_like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (model.isPraise()) {
//                    PhoneUtils.ShowToastMessage(ctx, "你已经点过赞了");
                    } else {
                        like(model.getInfoId());

                    }
                }
            });
        }


        private List<String> listimg = null;

        private void getPicUrl(CaseShowModel.MyList model) {
            listimg = new ArrayList<>();
            for (int i = 0; i < model.getResList().size(); i++) {
                if (model.getResList().get(i).getInfoResType().equals("pic")) {
                    listimg.add(model.getResList().get(i).getThumbnailUrl());
                    if (listimg.size() == 1) {
                        return;
                    }
                }
            }
        }

        private void like(int infoId) {
            String url = Constants.APP_API_BASE + Constants.OWN_PRAISECASESHOWS;
            RequestParams params = new RequestParams(url);
            params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(getContext()).getSession());
            params.addBodyParameter("infoId", infoId + "");
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    String flag = JsonUtil.getJsonValuesString(result, "flag");
                    if (flag.equals("true")) {
                        img_likes.setImageResource(R.mipmap.icon_like_high);
                        tv_like.setText("已赞");
                        tv_like.setTextColor(ctx.getResources().getColor(R.color.appThemeColor));
                    } else {
                    }

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }
}